package org.bozo.im.activity.account;

import java.util.ArrayList;

import org.bozo.im.R;
import org.bozo.im.activity.Constants;
import org.bozo.im.activity.ServiceMessenger;
import org.bozo.im.common.MessageContent;
import org.bozo.im.common.MessageType;
import org.bozo.im.common.types.AccountDesc;
import org.bozo.im.common.types.Protocol;
import org.bozo.im.common.types.ProtocolRequirement;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class AddAccountDialog extends AlertDialog {

	private OnProtocolSelectedListener lProtocolSelectedListener;
	
	private ProtocolAdapter lAdapter;
	private Context mContext;
	private ServiceMessenger mMessenger;
	private Messenger mHandler;

	private Spinner mProtocol;
	
	private EditText mLogin;
	private EditText mPassword;
	private EditText mServer;
	private EditText mPort;
	
	private Button mOk;
	private Button mCancel;
	
	public AddAccountDialog(Context pContext, ServiceMessenger pMessenger) {
		super(pContext);
		mContext = pContext;
		mHandler = new Messenger(new AddAccountHandler());
		lProtocolSelectedListener = new OnProtocolSelectedListener();
		mMessenger = pMessenger;
		mMessenger.getProtocols(mHandler);
	}

	@Override
	protected void onCreate(Bundle pSavedInstanceState) {
		super.onCreate(pSavedInstanceState);

		View.OnClickListener lListener = new OnButtonClickListener();
		
		mProtocol = (Spinner)findViewById(R.id.protocol);
		
		mLogin = (EditText)findViewById(R.id.login);
		mPassword = (EditText)findViewById(R.id.password);
		mServer = (EditText)findViewById(R.id.server);
		mPort = (EditText)findViewById(R.id.port);
		
		mOk = (Button)findViewById(R.id.ok);
		mCancel = (Button)findViewById(R.id.cancel);
		mOk.setOnClickListener(lListener);
		mCancel.setOnClickListener(lListener);
	}
	
	public ProtocolAdapter getProtocolSelectorAdapter() {

		if (lAdapter == null)
			lAdapter = new ProtocolAdapter(mContext, this);

		return lAdapter;
	}

	public void setProtocols(ArrayList<Protocol> pProtocols) {
		mProtocol.setAdapter(getProtocolSelectorAdapter());
		mProtocol.setOnItemSelectedListener(lProtocolSelectedListener);
		getProtocolSelectorAdapter().updateProtocols(pProtocols);
	}

	public void updateSelectedProtocol(int pSelection) {
		Protocol lProtocol = (Protocol)getProtocolSelectorAdapter().getItem(pSelection);
		mMessenger.getProtocolRequirements(lProtocol, mHandler);
	}

	public void updateProtocolRequirements(ArrayList<ProtocolRequirement> pRequirements) {

		if (pRequirements.contains(ProtocolRequirement.LOGIN_NAME)) {
			mLogin.setVisibility(View.VISIBLE);
		} else {
			mLogin.setVisibility(View.GONE);
		}
		
		if (pRequirements.contains(ProtocolRequirement.PASSWORD)) {
			mPassword.setVisibility(View.VISIBLE);
		} else {
			mPassword.setVisibility(View.GONE);
		}
		
		if (pRequirements.contains(ProtocolRequirement.SERVER)) {
			mServer.setVisibility(View.VISIBLE);
		} else {
			mServer.setVisibility(View.GONE);
		}
		
		if (pRequirements.contains(ProtocolRequirement.PORT)) {
			mPort.setVisibility(View.VISIBLE);
		} else {
			mPort.setVisibility(View.GONE);
		}
	}

	private class AddAccountHandler extends Handler {

		@Override
		public void handleMessage(Message pMsg) {

			pMsg.getData().setClassLoader(getClass().getClassLoader());
			
			switch (pMsg.what) {
			case MessageType.GET_PROTOCOLS_RESPONSE:
				
				// ---------------------------------------------------------------------------------------
				Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.activity.account.AddAccountDialog.AddAccountHandler.handleMessage ---------- ");
				// ---------------------------------------------------------------------------------------
				
				ArrayList<Protocol> lProtocols = pMsg.getData().getParcelableArrayList(MessageContent.PROTOCOLS);
				setProtocols(lProtocols);
				break;
				
			case MessageType.GET_PROTOCOL_REQUIREMENT_RESPONSE:
				ArrayList<ProtocolRequirement> lRequirements = pMsg.getData().getParcelableArrayList(MessageContent.PROTOCOL_REQUIREMENTS);
				updateProtocolRequirements(lRequirements);
				break;
			}

			super.handleMessage(pMsg);
		}
	}
	
	private class OnProtocolSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> pParent, View pView, int pPosition, long pId) {
			updateSelectedProtocol(pPosition);
		}

		public void onNothingSelected(AdapterView<?> pArg0) {}
			
	}

	private class OnButtonClickListener implements View.OnClickListener {

		public void onClick(View pV) {
			if (pV == mCancel) {
				dismiss();
			} else if (pV == mOk) {
				
				AccountDesc lDesc = new AccountDesc();
				
				if (mLogin.getVisibility() == View.VISIBLE) {
					lDesc.setLoginName(mLogin.getText().toString());
				}
				
				if (mPassword.getVisibility() == View.VISIBLE) {
					lDesc.setPassword(mPassword.getText().toString());
				}
				
				if (mServer.getVisibility() == View.VISIBLE) {
					lDesc.setServer(mServer.getText().toString());
				}
				
				if (mPort.getVisibility() == View.VISIBLE) {
					lDesc.setPort(Integer.valueOf(mPort.getText().toString()));
				}

				lDesc.setProtocol((Protocol)mProtocol.getSelectedItem());
				 
				mMessenger.addAccount(lDesc);
				dismiss();
			}
		}
	}

}
